home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / CBGRX103.ZIP / contrib / libgrx / drivers / vesainfo.c < prev    next >
Text File  |  1993-12-06  |  9KB  |  277 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <dos.h>
  4.  
  5. /*
  6.  * VESA BIOS extended function number and return code
  7.  */
  8. #define VESA_FUNC    0x4f00
  9. #define VESA_SUCCESS    0x004f
  10.  
  11. /*
  12.  * VESA BIOS sub-function numbers
  13.  */
  14. #define VESA_VGA_INFO    0
  15. #define VESA_MODE_INFO  1
  16. #define VESA_SET_MODE    2
  17. #define VESA_GET_MODE    3
  18. #define VESA_VGA_STATE  4
  19. #define VESA_PAGE_CTL    5
  20.  
  21. #define ushort  unsigned short
  22.  
  23. /*
  24.  * The VGA info structure
  25.  */
  26. typedef struct {
  27.     char       VESASignature[4];    /* should be "VESA" */
  28.     short      VESAVersion;        /* VESA version number */
  29.     char  far *OEMStringPtr;        /* Pointer to OEM string */
  30.     long       Capabilities;        /* capabilities of the video env */
  31.     short far *VideoModePtr;        /* ptr to supported Super VGA modes */
  32.     char       reserved[238];
  33. } VgaInfoBlock;
  34.  
  35. /*
  36.  * The mode info structure
  37.  */
  38. typedef struct {
  39.     short      ModeAttributes;        /* mode attributes */
  40.     char       WinAAttributes;        /* Window A attributes */
  41.     char       WinBAttributes;        /* Window B attributes */
  42.     short      WinGranularity;        /* window granularity */
  43.     short      WinSize;            /* window size */
  44.     ushort     WinASegment;        /* Window A start segment */
  45.     ushort     WinBSegment;        /* Window B start segment */
  46.     void far   (*WinFuncPtr)();        /* pointer to window function */
  47.     short      BytesPerScanLine;    /* bytes per scan line */
  48.     /*
  49.      * extended information
  50.      * optional information
  51.      */
  52.     short      XResolution;        /* horizontal resolution */
  53.     short      YResolution;        /* vertical resolution */
  54.     char       XCharSize;        /* character cell width */
  55.     char       YCharSize;        /* character cell height */
  56.     char       NumberOfPlanes;        /* number of memory planes */
  57.     char       BitsPerPixel;        /* bits per pixel */
  58.     char       NumberOfBanks;        /* number of banks */
  59.     char       MemoryModel;        /* memory model type */
  60.     char       BankSize;        /* bank size in K */
  61.     char       NumImagePages;        /* number of image pages */
  62.     char       reserved[1];
  63.     /*
  64.      * VESA 1.2 and later
  65.      */
  66.     char       RedMaskSize;        /* number of bits in red mask */
  67.     char       RedMaskPos;        /* starting bit for red mask */
  68.     char       GreenMaskSize;
  69.     char       GreenMaskPos;
  70.     char       BlueMaskSize;
  71.     char       BlueMaskPos;
  72.     char       ReservedMaskSize;    /* reserved bits in pixel */
  73.     char       ReservedMaskPos;
  74.     char       DirectScreenMode;
  75.     char       reserved2[1];
  76. } ModeInfoBlock;
  77.  
  78. /*
  79.  * MODE attribute bits
  80.  */
  81. #define MODE_SUPPORTED  1        /* Mode supported in hardware */
  82. #define MODE_EXTINFO    2        /* Extended information available */
  83. #define MODE_SUPBIOS    4        /* Outp functions supported by BIOS */
  84. #define MODE_ISCOLOR    8        /* Monochrome/color mode */
  85. #define MODE_ISGRAPHICS 16        /* Mode type (0: text, 1:graphics) */
  86.  
  87. /*
  88.  * Window attribute bits
  89.  */
  90. #define WIN_SUPPORTED    1        /* Window supported */
  91. #define WIN_READABLE    2        /* Window readable */
  92. #define WIN_WRITABLE    4        /* Window writable */
  93.  
  94. /*
  95.  * MemoryModel values
  96.  */
  97. #define MODEL_TEXT    0        /* 00h = Text mode */
  98. #define MODEL_CGA    1        /* 01h = CGA graphics */
  99. #define MODEL_HERC    2        /* 02h = Hercules graphics */
  100. #define MODEL_4PLANE    3        /* 03h = 4-plane planar */
  101. #define MODEL_PACKED    4        /* 04h = Packed pixel */
  102. #define MODEL_256_NC    5        /* 05h = Non-chain 4, 256 color */
  103. #define MODEL_DIRECT    6        /* 06h = direct color mode */
  104. /* 06h-0Fh = Reserved, to be defined by VESA */
  105. /* 10h-FFh = To be defined by OEM         */
  106.  
  107. int VESAversion;
  108. #define VESA_VERSION(major,minor)    ((major << 8) + minor)
  109.  
  110. int getinfo(VgaInfoBlock *vgainfo)
  111. {
  112.     _ES = FP_SEG(vgainfo);
  113.     _DI = FP_OFF(vgainfo);
  114.     _AX = VESA_FUNC + VESA_VGA_INFO;
  115.     geninterrupt(0x10);
  116.     if(_AX != VESA_SUCCESS) return(0);
  117.     if(vgainfo->VESASignature[0] != 'V') return(0);
  118.     if(vgainfo->VESASignature[1] != 'E') return(0);
  119.     if(vgainfo->VESASignature[2] != 'S') return(0);
  120.     if(vgainfo->VESASignature[3] != 'A') return(0);
  121.     VESAversion = vgainfo->VESAVersion;
  122.     return(1);
  123. }
  124.  
  125. int getmodeinfo(int mode,ModeInfoBlock *modeinfo)
  126. {
  127.     _ES = FP_SEG(modeinfo);
  128.     _DI = FP_OFF(modeinfo);
  129.     _CX = mode;
  130.     _AX = VESA_FUNC + VESA_MODE_INFO;
  131.     geninterrupt(0x10);
  132.     if(_AX != VESA_SUCCESS) return(0);
  133.     return(modeinfo->ModeAttributes & MODE_SUPPORTED);
  134. }
  135.  
  136.  
  137. void printinfo(VgaInfoBlock *vgainfo)
  138. {
  139.     char  far *sp = vgainfo->OEMStringPtr;
  140.     short far *mp = vgainfo->VideoModePtr;
  141.  
  142.     printf("VESASignature: \"%c%c%c%c\"\n",
  143.         vgainfo->VESASignature[0],
  144.         vgainfo->VESASignature[1],
  145.         vgainfo->VESASignature[2],
  146.         vgainfo->VESASignature[3]
  147.     );
  148.     printf("VESAVersion:\t%d.%d\n",
  149.         ((vgainfo->VESAVersion >> 8) & 0xff),
  150.         (vgainfo->VESAVersion & 0xff)
  151.     );
  152.     printf("OEMStringPtr:\t\"");
  153.     while(*sp != '\0') putchar(*sp++);
  154.     printf("\"\nCapabilities:\t%ld\n",vgainfo->Capabilities);
  155.     printf("VideoModePtr:\t%Fp\n",mp);
  156.     printf("Video Modes:\t");
  157.     while(*mp != (-1)) printf("0x%x ",*mp++);
  158.     printf("\n\n");
  159. }
  160.  
  161. #define DEFBIT(bit)    { bit, #bit }
  162. typedef struct {
  163.     int  bit;
  164.     char *name;
  165. } bitdef;
  166.  
  167. bitdef modeattrbits[] = {
  168.     DEFBIT(MODE_SUPPORTED),
  169.     DEFBIT(MODE_EXTINFO),
  170.     DEFBIT(MODE_SUPBIOS),
  171.     DEFBIT(MODE_ISCOLOR),
  172.     DEFBIT(MODE_ISGRAPHICS),
  173.     { 0 }
  174. };
  175.  
  176. bitdef winattrbits[] = {
  177.     DEFBIT(WIN_SUPPORTED),
  178.     DEFBIT(WIN_READABLE),
  179.     DEFBIT(WIN_WRITABLE),
  180.     { 0 }
  181. };
  182.  
  183. bitdef memorymodels[] = {
  184.     DEFBIT(MODEL_TEXT),
  185.     DEFBIT(MODEL_CGA),
  186.     DEFBIT(MODEL_HERC),
  187.     DEFBIT(MODEL_4PLANE),
  188.     DEFBIT(MODEL_PACKED),
  189.     DEFBIT(MODEL_256_NC),
  190.     DEFBIT(MODEL_DIRECT),
  191.     { 0 }
  192. };
  193.  
  194. void printbits(int value,bitdef *def)
  195. {
  196.     int prev = 0;
  197.  
  198.     while(def->bit != 0) {
  199.         if(value & def->bit) {
  200.         if(prev) printf(" | ");
  201.         printf(def->name);
  202.         prev = 1;
  203.         }
  204.         def++;
  205.     }
  206.     if(!prev) printf("0");
  207.     printf("\n");
  208. }
  209.  
  210. char *getmodelname(int model)
  211. {
  212.     static char temp[50];
  213.  
  214.     if(model < 0) return(sprintf(temp,"Invalid model [%d]",model),temp);
  215.     if(model <= MODEL_DIRECT) return(memorymodels[model].name);
  216.     if(model <= 15) return(sprintf(temp,"VESA model [0x%02x]",model),temp);
  217.     return(sprintf(temp,"OEM model [%0x2x]",model),temp);
  218. }
  219.  
  220. void printmodeinfo(int mode,ModeInfoBlock *modeinfo)
  221. {
  222.     printf("Mode 0x%x is supported\n",mode);
  223.     printf("  ModeAttributes:   ");
  224.     printbits(modeinfo->ModeAttributes,modeattrbits);
  225.     printf("  WinAAttributes:   ");
  226.     printbits(modeinfo->WinAAttributes,winattrbits);
  227.     printf("  WinBAttributes:   ");
  228.     printbits(modeinfo->WinBAttributes,winattrbits);
  229.     printf("  WinGranularity:   %d\n",modeinfo->WinGranularity);
  230.     printf("  WinSize:          %d\n",modeinfo->WinSize);
  231.     printf("  WinASegment:      0x%04x\n",modeinfo->WinASegment);
  232.     printf("  WinBSegment:      0x%04x\n",modeinfo->WinBSegment);
  233.     printf("  WinFuncPtr:       \%Fp\n",modeinfo->WinFuncPtr);
  234.     printf("  BytesPerScanLine: %d\n",modeinfo->BytesPerScanLine);
  235.     if(!(modeinfo->ModeAttributes & MODE_EXTINFO)) return;
  236.     printf("  XResolution:      %d\n",modeinfo->XResolution);
  237.     printf("  YResolution:      %d\n",modeinfo->YResolution);
  238.     printf("  XCharSize:        %d\n",modeinfo->XCharSize);
  239.     printf("  YCharSize:        %d\n",modeinfo->YCharSize);
  240.     printf("  NumberOfPlanes:   %d\n",modeinfo->NumberOfPlanes);
  241.     printf("  BitsPerPixel:     %d\n",modeinfo->BitsPerPixel);
  242.     printf("  NumberOfBanks:    %d\n",modeinfo->NumberOfBanks);
  243.     printf("  MemoryModel:      %d (%s)\n",modeinfo->MemoryModel,getmodelname(modeinfo->MemoryModel));
  244.     printf("  BankSize:         %d\n",modeinfo->BankSize);
  245.     printf("  NumImagePages     %d\n",modeinfo->NumImagePages);
  246.     if(VESAversion < VESA_VERSION(1,2)) return;
  247.     printf("  RedMaskSize:      %d\n",modeinfo->RedMaskSize);
  248.     printf("  RedMaskPos:       %d\n",modeinfo->RedMaskPos);
  249.     printf("  GreenMaskSize:    %d\n",modeinfo->GreenMaskSize);
  250.     printf("  GreenMaskPos:     %d\n",modeinfo->GreenMaskPos);
  251.     printf("  BlueMaskSize:     %d\n",modeinfo->BlueMaskSize);
  252.     printf("  BlueMaskPos:      %d\n",modeinfo->BlueMaskPos);
  253.     printf("  ReservedMaskSize: %d\n",modeinfo->ReservedMaskSize);
  254.     printf("  ReservedMaskPos:  %d\n",modeinfo->ReservedMaskPos);
  255.     printf("  DirectScreenMode: %d\n",modeinfo->ReservedMaskSize);
  256. }
  257.  
  258. void main(void)
  259. {
  260.     VgaInfoBlock  vgainfo;
  261.     ModeInfoBlock modeinfo;
  262.     short far *modeptr;
  263.     int mode;
  264.  
  265.     if(getinfo(&vgainfo)) {
  266.         printinfo(&vgainfo);
  267.         modeptr = vgainfo.VideoModePtr;
  268.         while((mode = *modeptr++) != (-1)) {
  269.         if(getmodeinfo(mode,&modeinfo)) printmodeinfo(mode,&modeinfo);
  270.         else printf("Mode 0x%x IS NOT SUPPORTED!\n",mode);
  271.         }
  272.     }
  273.     else printf("VESA BIOS extensions not found\n");
  274.     exit(0);
  275. }
  276.  
  277.